<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class AdminSaleController extends AdminbaseController {

        protected $m_sale_manager;
        protected $m_sale;

        function _initialize() {
            parent::_initialize();
            $this->m_sale_manager= D("Admin/SaleManager");
            $this->m_sale= D("Admin/Sale");
            $this->m_sale_good= D("Sale/SaleGood");
        }

        /**
         * 消费机构管理
         */
        public function index(){
            $keyword = I("request.keyword");
            $this->assign("keyword",$keyword);
            if(!empty($keyword)){
                $where['sale_name'] = array('like',"%$keyword%");
            }
            $s_smid = I("request.s_smid");
            $this->assign("s_smid",$s_smid);
            if(!empty($s_smid)){
                $where["sale_manager_id"] = $s_smid;
            }
            $sale_manager_list = $this->m_sale_manager->select();
            $this->assign("sale_manager_list",$sale_manager_list);

            if (session('ADMIN_ID') == '10') {
                $count=$this->m_sale->where($where)->withTrashed()->count();
            }else{
                $count=$this->m_sale->where($where)->count();
            }

            if(I("s_download") == 1){
                $export_list = $this->m_sale->field("*")
                    ->where($where)
                    ->order("sale_manager_id DESC,delete_time ASC,create_time DESC")
                    ->select();
                $export_title = array(
                    'sale_manage_name'=>'上级消费机构',
                    'sale_name'=>'消费机构名称',
                    'price'=>'累计消费金额',
                    'create_time'=>'创建时间',
                );
                foreach($export_list AS &$item){
                    $item["sale_manage_name"] = "";
                    if($item["sale_manager_id"]>0){
                        $sale_manager = $this->m_sale_manager->where(array("id"=>$item["sale_manager_id"]))->find();
                        $item["sale_manage_name"]  = $sale_manager["sale_manage_name"];
                    }
                }
                $excel = new \Common\Lib\excel();
                $excel->export_data('二级消费机构',$export_title,$export_list);exit;
            }

            $page = $this->page($count, 15);
            $list = $this->m_sale->field("*")
                ->where($where)
                ->order("create_time desc")
                ->limit($page->firstRow , $page->listRows);
            // 如果是管理员可以看到删除的
            if (session('ADMIN_ID') == '10') {
                $list = $list->withTrashed()->order("sale_manager_id DESC,delete_time ASC,create_time DESC");
            }

            $list = $list->select();
            foreach($list AS &$item){
                $item["sale_manage_name"] = "";
                if($item["sale_manager_id"]>0){
                    $sale_manager = $this->m_sale_manager->where(array("id"=>$item["sale_manager_id"]))->find();
                    $item["sale_manage_name"]  = $sale_manager["sale_manage_name"];
                }
                $sale_goods = $this->m_sale_good->where(array('sale_id'=>$item['id']))->select();
                $item['sale_goods'] = $sale_goods;
            }


            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }


        // 添加
        public function add(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_sale->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }
            $sale_manager_list = $this->m_sale_manager->select();
            $this->assign("sale_manager_list",$sale_manager_list);
            $this->display();
        }

        // 提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");
                $insert = I("post.");

                if(empty($id)){
                    $insert['create_time'] =time();
                    $result =$this->m_sale->add($insert);
                }else{

                    $result = $this->m_sale->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }

        // 删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_sale->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

        /*彻底删除就是把软删除关了，感觉理论上应该没错*/
        public function overdelete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_sale->delete($id,true)!==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }
        }

        /*彻底删除就是把软删除关了*/
        public function restore(){
            $id = I('get.id',0,'intval');

            $result = $this->m_sale->restore($id);

            if ($result) {
                $this->success("数据已恢复！");
            }else{
                $this->success("数据恢复失败！");
            }
        }
    }